import java.util.TreeMap;

/**
 * @author akaovi
 * @version 1.0
 * @description
 * @date 2022/11/14 15:56
 * @since 2022/11/14 15:56
 */
public class Two {

    private int count;

    public int getCount() {
        return count;
    }

    public Two(int count) {
        this.count = count;
    }

    public void change() {
        count += 1;
    }

    public static void main(String[] args) {
        boolean[] flag = {true, false, false};
        final Two count = new Two(1);

        Thread A = new Thread(() -> {
            while (true) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (flag[0]) {
                    System.out.printf("第%d次: A", count.getCount());
                    flag[0] = false;
                    flag[1] = true;
                }
            }
        });
        A.setName("A");

        Thread B = new Thread(() -> {
            while (true) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (flag[1]) {
                    System.out.print("B");
                    flag[1] = false;
                    flag[2] = true;
                }
            }
        });
        B.setName("B");

        Thread C = new Thread(() -> {
            while (true) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (flag[2]) {
                    System.out.println("C");
                    flag[2] = false;
                    if (count.getCount() < 10) {
                        flag[0] = true;
                        count.change();
                    } else {
                        break;
                    }
                }
            }
        });
        C.setName("C");

        A.start();
        B.start();
        C.start();

        while (true) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (count.getCount() >= 10) {
                System.exit(0);
            }
        }
    }
}
